#include "data.h"

Data::Data()
{
    cells = new QList<Cell*>;
    rules = new Rules();
}

void Data::clearData()
{
    foreach(Cell *c, *(this->cells))
    {
        c->releaseMemory();
        delete c;
    }

    cells->clear();
}

void Data::setScale(qreal *scale)
{
    foreach(Cell* cell, *cells)
    {
        foreach(Transistor* t, *(cell->nTransistors))
            t->scale = scale;

        foreach(Transistor* t, *(cell->pTransistors))
            t->scale = scale;

        foreach(Metal *m, *(cell->metalItems))
            m->scale = scale;

        foreach(Metal *m, *(cell->m1Items))
            m->scale = scale;

        foreach(Metal *m, *(cell->m2Items))
            m->scale = scale;

        foreach(Metal *m, *(cell->m1PinItems))
            m->scale = scale;

        foreach(Metal *m, *(cell->poItems))
            m->scale = scale;

        foreach(Metal *m, *(cell->coItems))
            m->scale = scale;

        foreach(Metal *m, *(cell->diffItems))
            m->scale = scale;

        foreach(Via* v, *(cell->vias))
            v->scale = scale;

        cell->vccStrapItem->scale = scale;
        cell->gndStrapItem->scale = scale;
    }
}

void Data::readSpiceFile(QString fileName, QPlainTextEdit *debug)
{
    //clearData();
    QFile file(fileName);

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        debug->appendHtml("<font color='red'>Failed openning: " + fileName + "</font>");
        return;
    }

    QTextStream in(&file);
    while (!in.atEnd())
    {
        QString token;
        in >> token;

        if(token.compare(".subckt",Qt::CaseInsensitive) == 0)
        {
            Cell *newCell = new Cell();
            in >> token;
            newCell->setCellName(token);
            debug->appendPlainText("Reading cell: " + token);

            QStringList pins = in.readLine().split(' ',QString::SkipEmptyParts);

            //reading pins
            foreach(QString pin, pins)
                newCell->addPin(pin);

            //reading transistors
            QStringList transistor = in.readLine().split(' ',QString::SkipEmptyParts);
            int indexNmos = 1;
            int indexPmos = 1;
            while(transistor.at(0).compare(".ends",Qt::CaseInsensitive)!=0)
            {
                Transistor* newT = new Transistor();

                newT->setTransistorName(transistor[0]);
                newT->setSourceNetName(transistor[1]);
                newT->setGateNetName(transistor[2]);
                newT->setDrainNetName(transistor[3]);
                newT->setBulkNetName(transistor[4]);
                newT->setTransistorType(transistor[5]);
                newT->flipped = false;

                if(newT->transistorType.compare("NMOS", Qt::CaseInsensitive) == 0)
                    newT->posIndex = indexNmos++;
                else
                    newT->posIndex = indexPmos++;

                QString l = transistor.at(6);
                l.remove("L=", Qt::CaseInsensitive);
                newT->setL(l.remove("u", Qt::CaseInsensitive).toDouble()*factor);

                QString w = transistor.at(7);
                w.remove("W=", Qt::CaseInsensitive);
                newT->setW(w.remove("u", Qt::CaseInsensitive).toDouble()*factor);
                QString line = in.readLine();
                transistor = line.split(' ',QString::SkipEmptyParts);

                newCell->addTransistor(newT);
            }
            this->addCell(newCell);
        }
    }
    debug->appendPlainText("Finished!");
    file.close();
}

void Data::addCell(Cell *c)
{
    this->cells->append(c);
}


